package com.wqtz.chechai.view.fragments;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.TimeAdapter;
import com.wqtz.chechai.base.BaseFragment;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.view.activities.TimeSelectActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/29 14:38
 */

public class TimeYearFragment extends BaseFragment implements TimeAdapter.itemClickCallBack {
	@BindView(R.id.lv)
	ListView mLv;
	@BindView(R.id.fm_month)
	FrameLayout mFmMonth;
	private int year;
	List<String> timelist;
	TimeAdapter mTimeAdapter;
	private String selectYear;

	@Override
	public BasePresent createPresenter() {
		return null;
	}

	@Override
	protected int getContentView() {
		return R.layout.fragment_buycaryear;
	}

	@Override
	protected void init(View view) {
		ButterKnife.bind(this, view);
		timelist = new ArrayList<>();
		Calendar a = Calendar.getInstance();
		year = a.get(Calendar.YEAR);
		for (int i = year - 30; i <= year; i++) {
			timelist.add(i + "年");
		}

	}

	@Override
	protected void loadDatas() {
		mTimeAdapter = new TimeAdapter(R.layout.item_time_select);
		mTimeAdapter.addData(timelist);
		mLv.setAdapter(mTimeAdapter);
		mTimeAdapter.setCallBack(this);

	}






	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO: inflate a fragment view
		View rootView = super.onCreateView(inflater, container, savedInstanceState);
		ButterKnife.bind(this, rootView);
		return rootView;
	}

	@Override
	public void itemClick(int position) {
		selectYear = timelist.get(position);
		//ToastUtils.show(getContext(), selectYear, 1000);
		FragmentManager fragmentManager = getChildFragmentManager();
		fragmentManager.beginTransaction().add(R.id.fm_month, new TimeMonthFragment(), "TimeMonthFragment").commit();
		TimeSelectActivity timeSelectActivity = (TimeSelectActivity) getActivity();
		timeSelectActivity.setYear(selectYear.substring(0,4));
		timeSelectActivity.setTvTitle("选择月份");
		mLv.setVisibility(View.GONE);
	}
}
